//
//  GameCollectionViewController.m
//  UI
//
//  Created by will on 16/3/23.
//  Copyright © 2016年 shangtv. All rights reserved.
//

#import "GameCollectionViewController.h"
#import "HJAlertView.h"
//#import "LZAlertView.h"

#define ColorAreaLine (3)
#define ColorAreaColumn (3)
#define ColorAreaInterval (5)

@interface GameCollectionViewController ()<UICollectionViewDelegateFlowLayout>


@property (strong, nonatomic) IBOutlet UICollectionView *collection;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;

@property(nonatomic,assign)NSInteger randomNum;

@property(nonatomic,assign)int red;
@property(nonatomic,assign)int green;
@property(nonatomic,assign)int blue;

@property(nonatomic,assign)int difficulty;
@end

@implementation GameCollectionViewController

static NSString * const reuseIdentifier = @"game";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    
    [self.collection registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:reuseIdentifier];
    self.difficulty = 1;
    
    
    self.randomNum = arc4random_uniform(8);
    self.red   = arc4random_uniform(255);
    self.green = arc4random_uniform(255);
    self.blue  = arc4random_uniform(255);
    
    self.collection.bounces = NO;
    
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.layout.itemSize = [self calculationColorAreaBounds];
    
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.layout.minimumLineSpacing = 8;
}


- (CGSize)calculationColorAreaBounds{
    CGSize range = [UIScreen mainScreen].bounds.size;
    
    CGFloat width  = range.width/ColorAreaColumn - 5 * (ColorAreaColumn - 1);;
    CGFloat height = range.height/ColorAreaLine - 5 * (ColorAreaLine -1);
    
    range = CGSizeMake(width, height);
    return range;
}


- (BOOL)prefersStatusBarHidden{
    return YES;
}

#pragma mark - Navigation


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
}


#pragma mark <UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  
    return ColorAreaColumn*ColorAreaLine;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:self.red/255.0 green:self.green/255.0 blue:self.blue/255.0 alpha:1];
    
    cell.layer.cornerRadius = 5.0;
    if (indexPath.item == self.randomNum) {
        
        cell.backgroundColor = [UIColor colorWithRed:(self.red-self.difficulty)/255.0 green:self.green/255.0f blue:self.blue/255.0f alpha:1];

    }
    
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [UIColor whiteColor].CGColor;
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.item == self.randomNum) {
        
        if (self.difficulty == 1) {
            HJAlertView  *view =  [[HJAlertView alloc]initWithTitle:@"恭喜通关" contentText:@"德玛西亚" leftButtonTitle:@"重新开始" rightButtonTitle:@"返回首页"];
           
            [view setLeftBtnBlock:^{
                self.difficulty = 25;
                [self.collection reloadData];
            }];
            
            [view setRightBtnBlock:^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            }];
            
            [view show];
        }
        self.difficulty--;
        
        
        self.randomNum = arc4random_uniform(8);
        NSLog(@"%zd",self.randomNum);
        self.red   = arc4random_uniform(255);
        self.green = arc4random_uniform(255);
        self.blue  = arc4random_uniform(255);
        
        [self.collection reloadData];
        
    }else{
        
        HJAlertView *view = [[HJAlertView alloc]initWithTitle:@"失败" contentText:@"再接再厉" leftButtonTitle:@"重新开始" rightButtonTitle:@"分享"];
        [view setRightBtnBlock:^{
            NSLog(@"点击了分享");
        }];
 
        
        [view setLeftBtnBlock:^{
            self.difficulty = 25;
            [self.collection reloadData];
        }];
        [view show];
     }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return  UIEdgeInsetsMake(5, 5, 5, 5);
}
@end
